package com.ruoyi.account.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.db.domain.AccountInfo;
import com.ruoyi.db.domain.AccountItemSpecial;
import com.ruoyi.db.mapper.AccountInfoMapper;
import com.ruoyi.db.mapper.AccountItemSpecialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.AccountItemSpecialHisMapper;
import com.ruoyi.db.domain.AccountItemSpecialHis;
import com.ruoyi.account.service.IAccountItemSpecialHisService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

/**
 * 机构账户流水Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-28
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AccountItemSpecialHisServiceImpl implements IAccountItemSpecialHisService 
{
    @Resource
    private AccountItemSpecialHisMapper accountItemSpecialHisMapper;
    @Resource
    private AccountItemSpecialMapper accountItemSpecialMapper;

    /**
     * 查询机构账户流水
     * 
     * @param id 机构账户流水主键
     * @return 机构账户流水
     */
    @Override
    public AccountItemSpecialHis selectAccountItemSpecialHisById(Long id)
    {
        return accountItemSpecialHisMapper.selectAccountItemSpecialHisById(id);
    }

    /**
     * 查询机构账户流水列表
     * 
     * @param accountItemSpecialHis 机构账户流水
     * @return 机构账户流水
     */
    @Override
    public List<AccountItemSpecialHis> selectAccountItemSpecialHisList(AccountItemSpecialHis accountItemSpecialHis)
    {
        return accountItemSpecialHisMapper.selectAccountItemSpecialHisList(accountItemSpecialHis);
    }

    /**
     * 查询机构账户流水列表
     *
     * @param accountItemSpecialHis 机构账户流水
     * @return 机构账户流水
     */
    @Override
    public List<Map<String, Object>> selectAccountItemSpecialHisListMap(AccountItemSpecialHis accountItemSpecialHis)
    {
        return accountItemSpecialHisMapper.selectAccountItemSpecialHisListMap(accountItemSpecialHis);
    }

    /**
     * 新增机构账户流水
     * 
     * @param accountItemSpecialHis 机构账户流水
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAccountItemSpecialHis(AccountItemSpecialHis accountItemSpecialHis)
    {
        AccountItemSpecial accountItemSpecial = accountItemSpecialMapper.selectAccountItemSpecialById(String.valueOf(accountItemSpecialHis.getAccountId()));
        accountItemSpecialHis.setDateInfo(LocalDate.now().toString());
        accountItemSpecialHis.setInfoId(String.valueOf(accountItemSpecial.getInfoId()));
        accountItemSpecialHis.setInfoName(String.valueOf(accountItemSpecial.getInfoName()));

        SysUser sysUser = ShiroUtils.getSysUser();
        accountItemSpecialHis.setOperatorId(String.valueOf(sysUser.getUserId()));
        accountItemSpecialHis.setOperatorName(String.valueOf(sysUser.getUserName()));

        // 当前余额
        BigDecimal currentAmount = NumberUtil.add(accountItemSpecial.getAvaliableAmount(), accountItemSpecialHis.getChangeAmount());

        accountItemSpecialHis.setOriginAmount(accountItemSpecial.getAvaliableAmount());
        accountItemSpecialHis.setCurrentAmount(currentAmount);
        accountItemSpecialHis.setActionType("ADJUST");
        accountItemSpecialHis.setCreateTime(DateUtils.getNowDate());
        int i = accountItemSpecialHisMapper.insertAccountItemSpecialHis(accountItemSpecialHis);

        if (i < 1){
            return i;
        }

        AccountItemSpecial updateEntity = new AccountItemSpecial();
        updateEntity.setId(String.valueOf(accountItemSpecialHis.getAccountId()));
        updateEntity.setAvaliableAmount(currentAmount);
        int i1 = accountItemSpecialMapper.updateAccountItemSpecial(updateEntity);
        if (i1 < 1){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return i1;
    }

    /**
     * 修改机构账户流水
     * 
     * @param accountItemSpecialHis 机构账户流水
     * @return 结果
     */
    @Override
    public int updateAccountItemSpecialHis(AccountItemSpecialHis accountItemSpecialHis)
    {
        return accountItemSpecialHisMapper.updateAccountItemSpecialHis(accountItemSpecialHis);
    }

    /**
     * 批量删除机构账户流水
     * 
     * @param ids 需要删除的机构账户流水主键
     * @return 结果
     */
    @Override
    public int deleteAccountItemSpecialHisByIds(String ids)
    {
        return accountItemSpecialHisMapper.deleteAccountItemSpecialHisByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除机构账户流水信息
     * 
     * @param id 机构账户流水主键
     * @return 结果
     */
    @Override
    public int deleteAccountItemSpecialHisById(Long id)
    {
        return accountItemSpecialHisMapper.deleteAccountItemSpecialHisById(id);
    }
}
